package editor.project;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;


/**
 * Finds the project, folder and file which are selected in the Project Tree
 * 
 * @author markdessain
 */
public class TreeSelection 
{
	
	// The selected project
	private Project selectedProject = null;
	private ProjectFile selectedFolder = null;   
	private ProjectFile selectedFile = null;       
	
	/**
	 * @param selPath the selected tree path
	 */
	public TreeSelection(TreePath selPath)
	{
		// If the select path is null or below 1 then no project is selected
		if(selPath != null && selPath.getPathCount() > 1)
		{
			// Create a project for the project which is selected
			DefaultMutableTreeNode projectNode = (DefaultMutableTreeNode)(selPath.getPathComponent(1)); 
			selectedProject = (Project)projectNode.getUserObject();
				
			// If the select path is below 2 then no file/folder is selected
			if(selPath.getPathCount() > 2)
			{
				// Create a file for the file which is selected
				projectNode = (DefaultMutableTreeNode)(selPath.getLastPathComponent()); 
				ProjectFile projectFile = ((ProjectFile)projectNode.getUserObject());
				
				selectedFile = projectFile;
				
				if(projectFile.isDirectory())
				{
					selectedFolder = projectFile;
				}
				else
				{					
					DefaultMutableTreeNode selectedParent = (DefaultMutableTreeNode)(selPath.getPathComponent(selPath.getPathCount() - 2));
					selectedFolder = ((ProjectFile)selectedParent.getUserObject());	
				}
			}
		}
	}
		
	/**
	 * @return the selected project
	 */
	public Project getProject()
	{
		return selectedProject;
	}
	
	/**
	 * @return the selected folder
	 */
	public ProjectFile getFolder()
	{
		return selectedFolder;
	}
	
	/**
	 * @return the selected file
	 */
	public ProjectFile getFile()
	{
		return selectedFile;
	}
	
}